local chidi = fk.CreateSkill{
  name = "hanqing__chidi",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["hanqing__chidi"] = "赤帝",
  [":hanqing__chidi"] = "锁定技，当你造成伤害后，你将<a href=':chixiao_sword'>【赤霄剑】</a>置入武器栏。"..
  "当你失去【赤霄剑】后，你发动〖归心〗。当你翻回正面后，你从弃牌堆获得一张点数为7的牌。",

  ["#hanqing__chidi-prey"] = "赤帝：获得其中一张牌",
}

chidi:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(chidi.name) and player:hasEmptyEquipSlot(Card.SubtypeWeapon) then
      local room = player.room
      local id
      if room:getBanner(chidi.name) == nil then
        id = room:printCard("chixiao_sword", Card.Heart, 7).id
        room:setBanner(chidi.name, {id})
      else
        id = room:getBanner(chidi.name)[1]
      end
      if table.contains(player:getCardIds("h"), id) then
        return true
      elseif not table.contains(player:getEquipments(Card.SubtypeWeapon), id) and
        table.contains({ Card.Void, Card.DiscardPile, Card.PlayerEquip }, room:getCardArea(id)) then
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local id = room:getBanner(chidi.name)[1]
    room:moveCardIntoEquip(player, id, chidi.name, false, player)
  end,
})

chidi:addEffect(fk.AfterCardsMove, {
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(chidi.name) and
      table.find(player.room.alive_players, function (p)
        return p ~= player and not p:isAllNude()
      end) then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip and Fk:getCardById(info.cardId).name == "chixiao_sword" then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = player.room:getOtherPlayers(player, false)})
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not p.dead and not p:isAllNude() then
        local id = room:askToChooseCard(player, {
          target = p,
          flag = "hej",
          skill_name = chidi.name,
        })
        room:obtainCard(player, id, false, fk.ReasonPrey, player, chidi.name)
        if player.dead then return end
      end
    end
    player:turnOver()
  end,
})

chidi:addEffect(fk.TurnedOver, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(chidi.name) and player.faceup and
      table.find(player.room.discard_pile, function (id)
        return Fk:getCardById(id).number == 7
      end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = table.filter(room.discard_pile, function (id)
      return Fk:getCardById(id).number == 7
    end)
    local card = room:askToChooseCard(player, {
      target = player,
      flag = { card_data = {{ "pile_discard", cards }} },
      skill_name = chidi.name,
      prompt = "#hanqing__chidi-prey",
    })
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, chidi.name, nil, true, player)
  end,
})

return chidi
